﻿using System;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Subgurim.Controles;
using Data;
using Business;
using System.Collections.Generic;

namespace GestorEventos
{
    public partial class addPalco : System.Web.UI.Page
    {
        static double longitude;
        static double latitude;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["userID"] != null)
            {

                GMap1.enableServerEvents = true;
                GMap1.enableGoogleBar = true;
                GMap1.enableHookMouseWheelToZoom = true;
                GMap1.enableGKeyboardHandler = true;
                GMap1.addControl(new GControl(GControl.extraBuilt.MarkCenter));
                //GMap1.addControl(new GControl(GControl.extraBuilt.TextualCoordinatesControl));
                GMap1.setCenter(new GLatLng(40, -8), 6);
                GMap1.addControl(new GControl(GControl.preBuilt.MapTypeControl, new GControlPosition(GControlPosition.position.Bottom_Right)));
                GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl, new GControlPosition(GControlPosition.position.Top_Right)));

                Palco business = new Palco();
                List<String> listaCategorias = business.getCategoriasPalco();



                for (int i = 0; i < listaCategorias.Count; i++)
                {
                    DropDownList1.Items.Add(listaCategorias[i]);
                }
            }
            else {
                Response.Redirect("Default.aspx");
            } 
        }



        protected string GMap1_Click(object s, GAjaxServerEventArgs e)
        {
            GMarker marker = new GMarker(e.point);

            GInfoWindow window = new GInfoWindow(marker,
                string.Format(@"
        <b>Coordenadas</b><br />
        Latitude = {0}<br/>
        Longitude = {1}
        ",
                e.point.lat.ToString(),
                e.point.lng.ToString())
            , true);
            latitude = e.point.lat;
            longitude = e.point.lng;

            return window.ToString(e.map);
        }
        
        protected string GMap1_MarkerClick(object s, GAjaxServerEventArgs e)
        {
            string markerID = GMap1.getGMapElementById(e.who);
            string js = string.Format(@"
    if ({0}.isHidden())
        {0}.show()
    else
        {0}.hide();
    ", markerID);
 
            return js;
        } 

        protected void Confirmarb_Click(object sender, EventArgs e)
        {
            String nome = this.Nometb.Text;
            String desc = this.Desctb.Text;
            String cat = DropDownList1.SelectedItem.Text;
            String loc = this.Localidadetb.Text;
            String rua = this.Ruatb.Text;
            String codPostal = this.CodPostaltb.Text;

            Palco p = new Palco(nome, desc, cat, rua, codPostal, loc, (float)(float)latitude, (float)longitude);
            int value = p.insertPalco();

            if (value>0)
            {
                Response.Redirect("showPalco.aspx?palcoID=" + p.getIDPalco() + "");

            }
            else {
                Response.Redirect("addPalco.aspx");
            }

        }

        protected void Verificarb_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.Append(this.Ruatb.Text + ", " + this.Localidadetb.Text);
            float lat, longi;
            HttpWebRequest a = (HttpWebRequest)WebRequest.Create("http://maps.google.com/maps/geo?q=" + s.ToString() + "&output=csv&oe=utf8&sensor=true&key=ABQIAAAAp9PE6dkYqHKQfkAlDcxiTxRqasZdLy4IhB39S8xAqTahPYA7dxToPocIAu-_Po_hv5Hv1kc2-8gGvQ");
            HttpWebResponse resp = (HttpWebResponse)a.GetResponse();
            Stream st = resp.GetResponseStream();
            StreamReader sr = new StreamReader(st);
            string buffer = sr.ReadToEnd();
            String[] items = buffer.Split(',');
            if (items[0].Equals("200"))
            {
                lat = float.Parse(items[2].Replace(".", ","));
                longi = float.Parse(items[3].Replace(".", ","));
            }
            else
            {
                lat = -200;
                longi = -200;
            }
            
            GMap1.setCenter(new GLatLng((double) lat, (double) longi),16);
        }

       
    }
}
